#include "instruct.h"
#include "register.h"
#include "natives.h"
#include "def.h"
#include <string.h>
#include <stdio.h>

int callIsRun(char opt){
    if(opt==CALLN||opt==CALL){
        return 5;
    }
    return 0;
}
int callParseRun(Register*R){
    char*code=R->code;
    int pc=R->RPC;
    char opt=code[pc];
    if(opt==CALLN){
        u32 num2=byte2int(code[pc+4],code[pc+3],code[pc+2],code[pc+1]);
		natives*funList=(natives*)R->funList;
		u32 size=R->funSize;
		for(int i=0;i<size;i++){
			natives nv=funList[i];
			if(strcmp(nv.name,code+num2)==0){
				nv.func(R);
			}
			
		}
		R->RPC+=callIsRun(opt);
    }else if(opt==CALL){
		u32 num2=byte2int(code[pc+4],code[pc+3],code[pc+2],code[pc+1]);
		int index=R->RSS+R->RSP;
		int temp=R->RPC+5;
		code[index+0]=temp&0xFF;
		code[index+1]=(temp>>8)&0xFF;
		code[index+2]=(temp>>16)&0xFF;
		code[index+3]=(temp>>24)&0xFF;
		R->RSP=R->RSP+4;
		R->RPC=num2;
	}
    
    return 0;
}